var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../services/user.js');
var app = getApp();
var that;
Page({
  data: {
    showDialog: false,
    code: '',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    jumpPage: '',
    needJump: false,
  },
  onLoad: function (options) {
    that = this;
    //util.confirm.modal(JSON.stringify(options));
    if (options.page) {
      that.setData({
        jumpPage: decodeURIComponent(options.page),
        needJump: true,
      });
    }
  },
  checkLogin(){
    util.login().then((res) => {
      app.postForm(api.CheckLogin, { code: res.code, isAllow:true}).then(rsp => {
        //wx.clearStorage(); //清除缓存
        let res = rsp.data;
        //是否授权基本信息(微信头像，昵称)
        if (res.isAuthBaseInfo) {
          app.globalData.isAuthBaseInfo = true;
          app.globalData.userInfo = res.userInfo;
          wx.setStorageSync('userInfo', res.userInfo);
          wx.setStorageSync('isAuthBaseInfo', true);
        } else {
          wx.setStorageSync('isAuthBaseInfo', false);
        }
        //实名是否认证成功
        app.globalData.isCert = res.isCert;
        wx.setStorageSync('isCert', res.isCert);

        wx.setStorageSync('isTouristRent', res.isTouristRent);
        app.isTouristRent = res.isTouristRent;
        wx.setStorageSync('phoneNumber', res.phoneNumber);
        wx.setStorageSync('tenantNo', res.tenantNo);
        wx.setStorageSync('token', res.token);
        app.globalData.phoneNumber = res.phoneNumber;
        app.globalData.tenantNo = res.tenantNo;
        app.globalData.hasLogin = true;
        console.log('go into index...');
        if (that.data.needJump && that.data.jumpPage.indexOf('pages/index/index') == -1) {
          wx.redirectTo({
            url: '/' + that.data.jumpPage,
          })
        } else {
          wx.switchTab({
            url: '/pages/index/index',
          })
        }
      }).catch((err) => {
        console.log("检测登录", err);
        that.refreshCode();
      });
    }).catch((err) => {
      console.log("未登录报异常：", err);
      that.refreshCode();
    });
  },
  goLogin(){
    var url = '/pages/auth/accLogin/login';
    if (that.data.needJump) {
      url += '?page=' + encodeURIComponent(that.data.jumpPage);
    }
    wx.redirectTo({
      url: url,
    })
  },
  openDialog(){
    this.setData({ showDialog: true });
  },
  closeDialog: function () {
    this.setData({ showDialog: false });
  },
  onReady: function () {
    // 页面隐藏
  },
  onShow: function () {
  },
  onHide: function () {
  },
  onUnload: function () {
    // 页面关闭
  },
  refreshCode:function(){
    util.login().then((res) => {
      that.setData({
        code: res.code
      });
    }).catch((err) => {
      console.log("未登录报异常：", err);
    });
  },
  wxLogin: function (e) {
    let phoneInfo = e.detail;
    if (phoneInfo.errMsg != "getPhoneNumber:ok") {
      var url = '/pages/auth/bindPhone/bindphone';
      if (that.data.needJump) {
        url = '/pages/auth/bindPhone/bindphone?page=' + encodeURIComponent(that.data.jumpPage);
      }
      wx.redirectTo({
        url: url,
      })
      return;
    }
    phoneInfo.code = that.data.code;
    phoneInfo.isAllow = true;
    app.post(api.LoginByPhone, phoneInfo).then(rsp => {
      wx.clearStorage(); //清除缓存
      let res = rsp.data;
      //是否授权基本信息(微信头像，昵称)
      if (res.isAuthBaseInfo) {
        app.globalData.isAuthBaseInfo = true;
        app.globalData.userInfo = res.userInfo;
        wx.setStorageSync('userInfo', res.userInfo);
        wx.setStorageSync('isAuthBaseInfo', true);
      } else {
        wx.setStorageSync('isAuthBaseInfo', false);
      }
      //实名是否认证成功
      app.globalData.isCert = res.isCert;
      wx.setStorageSync('isCert', res.isCert);
      
      wx.setStorageSync('isTouristRent', res.isTouristRent);
      app.isTouristRent = res.isTouristRent;
      wx.setStorageSync('phoneNumber', res.phoneNumber);
      wx.setStorageSync('tenantNo', res.tenantNo);
      wx.setStorageSync('token', res.token);
      app.globalData.phoneNumber = res.phoneNumber;
      app.globalData.tenantNo = res.tenantNo;
      app.globalData.hasLogin = true;
      console.log('go into index...');
      if (that.data.needJump && that.data.jumpPage.indexOf('pages/index/index') == -1) {
        wx.redirectTo({
          url: '/' + that.data.jumpPage,
        })
      } else {
        var p = getCurrentPages()[0];
        if (!p.data.isUpdate) {
          p.setData({ isUpdate: true });
        }
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    }).catch((err) => {
      console.log('err', err);
      that.refreshCode();
    });
  },
  wxLoginByUserInfo: function (e) {
    if (e.detail.userInfo == undefined) {
      util.toast.fail('微信授权失败');
    } else {
      util.getCheckUserInfo().then(res => {
        console.log('userInfo', res);
        wx.getStorageSync('userInfo', res.userInfo);
        app.globalData.userInfo = res.userInfo;
        res.code = that.data.code;
        app.post(api.LoginByPhone, res).then(res => {
          console.log(res)
        }).catch((err) => {
          console.log('err', err);
        });
      });

    }
  }
})